package seatiger.tomcat.catalina.startup;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import org.xml.sax.Attributes;
import seatiger.tomcat.catalina.Lifecycle;
import seatiger.tomcat.catalina.LifecycleListener;

/**
 * 为容器添加 监听器
 */
public class LifecycleListenerRule<T extends LifecycleListener> extends Rule {

    private Class<T> clazz;

    public LifecycleListenerRule(Digester digester, Class<T> clazz) {
        super.setDigester(digester);
        this.clazz = clazz;
    }

    @Override
    public void begin(Attributes attributes) throws Exception {
        LifecycleListener listener = clazz.newInstance();
        Lifecycle lifecycle = (Lifecycle) digester.peek();
        lifecycle.addLifecycleListener(listener);
    }
}
